// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * A Command class for retrieving a list of blog posts.
 */

goog.provide('rb.post.GetPostsCommand');

goog.require('rb.post.BlogPost');

goog.require('relief.rpc.Command');



/**
 * A command class for retrieving a list of blog posts.  Exactly what details
 * and the number of posts retrieved is configurable via constructor arguments.
 *
 * The results of the query will never be cached, but if the full blog post
 * contents are retrieved, each individual post will be cached.
 *
 * @param {function(Array.<rb.post.BlogPost>)} onSuccess Callback function for
 *    a successful request.  The function will be passed an array of BlogPost
 *    objects, sorted from most to least recent.
 * @param {function()} onFailure Callback for a failed request.
 * @param {number} qty The number of posts to retrieve.
 * @param {boolean=} contents Whether to fetch the whole post or just a summary.
 *    If false or omitted, the contents are not fetched.  If true, the post
 *    contents will be retrieved also.
 *
 * @constructor
 * @extends {relief.rpc.Command}
 */
rb.post.GetPostsCommand = function(onSuccess, onFailure, qty, contents) {
  goog.base(this, onSuccess, onFailure,
            'GetList:' + qty + (contents ? ':contents:' : ':') + goog.now(),
            '/api/post/list/' + qty + (contents ? '/contents' : ''), 'GET');

  /**
   * @type {boolean}
   * @private
   */
  this.contents_ = !!contents;

  /**
   * @type {Array.<{rb.post.BlogPost}>}
   * @private
   */
  this.posts_ = null;

  // Only cache the returned blog posts if we get the full things.
  this.writeToCache = !!contents;
};
goog.inherits(rb.post.GetPostsCommand, relief.rpc.Command);


/**
 * @inheritDoc
 */
rb.post.GetPostsCommand.prototype.onSuccess = function(event) {
  var response = /** @type {Array.<rb.post.BlogPost.ServerResponse>} */
                 (event.target.getResponseJson()),
      posts = [];

  for (var i = 0, len = response.length; i < len; ++i) {
    posts[i] = rb.post.BlogPost.fromServerResponse(response[i]);
  }

  this.callersOnSuccess(posts);

  // Posts are only cached if we got the post contents.  If we didn't, then
  // we don't need to store a list of them.
  if (this.contents_) {
    this.posts_ = posts;
  }
};


/**
 * @inheritDoc
 */
rb.post.GetPostsCommand.prototype.onFailure = function(event) {
  this.callersOnFailure();
};


/**
 * @inheritDoc
 */
rb.post.GetPostsCommand.prototype.getCacheValues = function() {
  var posts = this.posts_,
      toCache = [];
  if (posts) {
    for (var i = 0, len = posts.length; i < len; ++i) {
      var post = /** @type {rb.post.BlogPost} */ (posts[i]),
          key = post.getCacheKey();

      if (key) {
        toCache[i] = {
          key: key,
          value: post
        };
      }
    }
  }

  return toCache;
};


/**
 * @inheritDoc
 */
rb.post.GetPostsCommand.prototype.disposeInternal = function() {
  this.posts_ = null;
};
